package com.yangjian.configure;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.yangjian.bean.Employee;
import com.yangjian.util.BaseContext;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

/**
 * 这是一个元素处理器类
 */

@Component
public class MyMataObjectHandler implements MetaObjectHandler {


   // @Autowired
   //private HttpSession session;


    /**
     * 插入元对象字段填充（用于插入时对公共字段的填充）
     *
     * @param metaObject 元对象
     */
    @Override
    public void insertFill(MetaObject metaObject) {

        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());
        //从session作用域中取值
        //Employee employee = (Employee) session.getAttribute("employee");
        //metaObject.setValue("createUser", employee.getId());
        //metaObject.setValue("updateUser",employee.getId());


        metaObject.setValue("createUser", BaseContext.getCurrentId());
        metaObject.setValue("updateUser",BaseContext.getCurrentId());
    }

    /**
     * 更新元对象字段填充（用于更新时对公共字段的填充）
     *
     * @param metaObject 元对象
     */
    @Override
    public void updateFill(MetaObject metaObject) {

        metaObject.setValue("updateTime", LocalDateTime.now());
        //从session作用域中取值
        //Employee employee = (Employee) session.getAttribute("employee");
        //metaObject.setValue("updateUser",BaseContext.getCurrentId());


        metaObject.setValue("updateUser",BaseContext.getCurrentId());

    }
}
